﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Text.RegularExpressions;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Currency.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 100;
    public string strPager = "";
    public string strIDs = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public int nPage = 1;
    public string strPageID = "page_number";
    public string strPartQuery = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Currency"] != null && int.Parse(Session["Currency"].ToString()) > 0)
        {
            if (Session["Currency"] != "") nPermissions = int.Parse(Session["Currency"].ToString());
        }
        if (nPermissions < 1)
            Response.Redirect("Login.aspx");
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["action"] == "delete")
            {
                Currency obj = new Currency();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Load(GV.cn, GV.trans);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("Currency.aspx");
            }
            if (nPermissions < 3) btnSave.Visible = false;
            #region Build List
            int nNumOfPages = 0;

            DataTable dtList;
            BuildList(NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
            if (nPage > nNumOfPages) nPage = 1;

            rptData.DataSource = dtList;
            rptData.DataBind();
            #endregion
            #region Build Pager
            CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
            #endregion
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        Currency obj = null;
        GV.ErrorMessage = "";
        foreach (RepeaterItem item in rptData.Items)
        {
            obj = new Currency();
            string strID = ((HiddenField)item.FindControl("txtID")).Value.Trim();
            string strCode = ((TextBox)item.FindControl("txtCode")).Text.Trim();
            string strName = ((TextBox)item.FindControl("txtName")).Text.Trim();
            string strPerVND = ((TextBox)item.FindControl("txtPerVND")).Text.Trim();
            float fVND = 0;
            if (strID.Length > 0)//Update
            {
                obj.ID = long.Parse(strID);
                obj.Load(GV.cn, GV.trans);
                obj.Code = strCode;
                obj.Name = strName;
                try
                {
                    fVND = float.Parse(strPerVND);
                }
                catch { }
                obj.PerVND = fVND;
                if (strCode.Length > 0)
                {
                    obj.Save(GV.cn, GV.trans);
                }

            }
            if (strCode.Length > 0 && strID.Length == 0)
            {
                obj.ID = -1;
                obj.Code = strCode;
                obj.Name = strName;
                try
                {
                    fVND = float.Parse(strPerVND);
                }
                catch { }
                obj.PerVND = fVND;
                obj.Save(GV.cn, GV.trans);
                Response.Redirect("Currency.aspx");
            }
        }

    }

    private void BuildList(int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("Index");
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Code");
        dtList.Columns.Add("Name");
        dtList.Columns.Add("PerVND");
        dtList.Columns.Add("Action");
        //Get List by SoIDs
        nNumOfPages = 0;
        strCondition = "";
        Currency.GetList(strCondition, "code", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        string strAllQueryString = "";
        string[] strValues = new string[] { "", "", "", "", "", "" };
        dtList.Rows.Add(strValues);
        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                count++;
                //string strSTT = dr["Sort"].ToString();
                string strIndex = count.ToString();
                string ID = dr["ID"].ToString();
                string Code = dr["Code"].ToString();
                string Name = dr["Name"].ToString();
                string PerVND = double.Parse(dr["PerVND"].ToString()).ToString("#,#.##");
                string strAction = "";
                if (nPermissions > 7)
                {
                    if (strAction.Length > 0) strAction += " - ";
                    strAction += "<a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "')\">DEL</a>";
                }
                strValues = new string[] { strIndex, ID, Code, Name, PerVND, strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
}
